package com.example.yls.demoa;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private static final int GG = 1001;
    private TextView txt_show;
    private EditText edit_phone;
    private Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt_show=findViewById(R.id.txt_show);
        edit_phone=findViewById(R.id.edit_phone);

        handler=new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {

                if (message.what==GG){
                    String result= (String) message.obj;
                         txt_show.setText(result);
                }

                return false;
            }
        });




        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {



                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String phone=edit_phone.getText().toString();
                        useHttpGet(phone);

                    }

                }).start();
            }
        });




       findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        useHttpPost();
                    }
                }).start();
           }
       });




    }

    private void useHttpPost() {

        String url="http://www.gdcp.cn";
        HttpClient httpClient=new DefaultHttpClient();
        HttpPost httpPost=new HttpPost(url);
        try {
            HttpResponse httpResponse=httpClient.execute(httpPost);
            int code=httpResponse.getStatusLine().getStatusCode();
            ProtocolVersion protocolVersion=httpResponse.getStatusLine().getProtocolVersion();
            String protocol=protocolVersion.getProtocol()+protocolVersion.getMajor()+"_"+protocolVersion.getMinor();
            Log.e(TAG, "code="+code+"    protocol:" +protocol);


            InputStream inputStream=httpResponse.getEntity().getContent();
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer stringBuffer=new StringBuffer();
            String line=bufferedReader.readLine();
            while (line!=null){
                stringBuffer.append(line);
                line=bufferedReader.readLine();

            }
            Log.e(TAG, "content:="+stringBuffer);


        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    private void useHttpGet(String phone) {
        String baseUrl="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
        String url=baseUrl.concat("?mobileCode=").concat(phone).concat("&userID=");
        HttpClient httpClient=new DefaultHttpClient();
        HttpGet httpGet=new HttpGet(url);
        try {
            HttpResponse httpResponse=httpClient.execute(httpGet);
            int code=httpResponse.getStatusLine().getStatusCode();
            ProtocolVersion protocolVersion=httpResponse.getStatusLine().getProtocolVersion();
            String protocol=protocolVersion.getProtocol()+protocolVersion.getMajor()+"_"+protocolVersion.getMinor();
            Log.e(TAG, "code="+code+"    protocol:" +protocol);


            InputStream inputStream=httpResponse.getEntity().getContent();
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer stringBuffer=new StringBuffer();
            String line=bufferedReader.readLine();
            while (line!=null){
                stringBuffer.append(line);
                line=bufferedReader.readLine();

            }
            Log.e(TAG, "content:="+stringBuffer);

            Message message=Message.obtain();
            message.what=GG;
            message.obj=stringBuffer.toString();
            handler.sendMessage(message);

        } catch (IOException e) {
            e.printStackTrace();
        }



    }
}
